JAVA小白的学习记录.8

您所在的位置:网站首页 javacompareto 排序 JAVA小白的学习记录.8

JAVA小白的学习记录.8

2023-03-28 22:53| 来源: 网络整理| 查看: 265

1.day181.1List集合中存自定义对象

现在集合容器中存数据的时候,字符串 整型 等

但是也可以存对象,集合中带有泛型 是一个类

package com.qf.b_list; import java.util.ArrayList; import java.util.List; class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override//重写的toString方法 Object类 //将内存地址 转为一个字符串表示的形式 可以看到打印的数据 不再是一个内存地址了 //而是一个字符串了 public String toString() { return "[name:" + name + ", age:" + age + "]"; } } public class Demo1 { public static void main(String[] args) { List persons = new ArrayList(); Person person1 = new Person("狗蛋", 24); Person person2 = new Person("张三", 14); Person person3 = new Person("王五", 34); persons.add(person1); persons.add(person2); persons.add(person3); System.out.println(persons); //以上在存数据 //可以取数据 // System.out.println(persons.get(0));//获取的是集合中第一个对象 // System.out.println(persons.get(1)); // System.out.println(persons.get(2)); // // //想只把狗蛋取出来 // //先取出来第一个person对象 // System.out.println(persons.get(0).getName()); for (Person person : persons) { System.out.println(person.getName() + ":" + person.getAge()); } } } 1.2ArrayList源码

ArrayList底层是数组

RandomAccess :这个接口可以让ArrayList拥有快速随机访问的能力

源码: for循环比迭代器速度更快的

package com.qf.c_arrayList; import java.awt.List; import java.util.ArrayList; import java.util.ListIterator; public class Demo1 { public static void main(String[] args) { ArrayList list = new ArrayList(); for (int i = 0; i < 1000000; i++) { list.add("a"); } //更快点 long start = System.currentTimeMillis(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } long end = System.currentTimeMillis();//12018毫秒 System.out.println(end - start); // ListIterator listIterator = list.listIterator(); // while (listIterator.hasNext()) { // System.out.println(listIterator.next()); // }//15075 } } /** * Default initial capacity. */ // DEFAULT_CAPACITY 默认的容量= 10 private static final int DEFAULT_CAPACITY = 10; /** * Shared empty array instance used for empty instances. */ //空的数组 Object[] private static final Object[] EMPTY_ELEMENTDATA = {}; /** * Shared empty array instance used for default sized empty instances. We * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when * first element is added. */ //m默认的构造方法中空数组 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. Any * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA * will be expanded to DEFAULT_CAPACITY when the first element is added. */ //真正存储元素的数组Object[] elementData transient Object[] elementData; // non-private to simplify nested class access /** * The size of the ArrayList (the number of elements it contains). * * @serial */ //最大数组的容量 private int size;

集合默认的可以存10个

/** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }

构造方法是干嘛的? new ArrayList();

elementData 数组 默认是10

public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } }

new ArrayList(4);

public ArrayList(Collection


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3